//
//  NSDictionary+Extensions.m
//  CantripSDK
//
//  Created by James Callender on Sun Apr 27 2003.
//  Copyright (c) 2003 James Callender. All rights reserved.
//

#import "NSDictionary+Extensions.h"


@implementation NSDictionary (CantripExtensions)

- (float)floatForKey:(NSString *)key defaultValue:(float)defaultValue;
{
    id value;

    value = [self objectForKey:key];
    if (value)
        return [value floatValue];
    return defaultValue;
}

- (float)floatForKey:(NSString *)key;
{
    return [self floatForKey:key defaultValue:0.0];
}

- (double)doubleForKey:(NSString *)key defaultValue:(double)defaultValue;
{
    id value;

    value = [self objectForKey:key];
    if (value)
        return [value doubleValue];
    return defaultValue;
}

- (double)doubleForKey:(NSString *)key;
{
    return [self doubleForKey:key defaultValue:0.0];
}

- (BOOL)boolForKey:(NSString *)key defaultValue:(BOOL)defaultValue;
{
    id value;

    value = [self objectForKey:key];
    if (!value)
        return defaultValue;

    if (![value isKindOfClass:[NSString class]]) {
        if ([value isKindOfClass:[NSNumber class]])
            return [value boolValue];
        // Should maybe raise an error here?
        return NO;
    }

    return [value boolValue];
}

- (BOOL)boolForKey:(NSString *)key;
{
    return [self boolForKey:key defaultValue:NO];
}

- (int)intForKey:(NSString *)key defaultValue:(int)defaultValue;
{
    id value;

    value = [self objectForKey:key];
    if (!value)
        return defaultValue;
    return [value intValue];
}

- (int)intForKey:(NSString *)key;
{
    return [self intForKey:key defaultValue:0];
}

@end
